package com.CampusForum.community.service.impl;

import com.CampusForum.community.dao.DiscussPostMapper;
import com.CampusForum.community.dao.UserMapper;
import com.CampusForum.community.entity.DiscussPost;
import com.CampusForum.community.entity.User;
import com.CampusForum.community.service.DiscussPostService;
import com.CampusForum.community.util.CommunityUtil;
import com.CampusForum.community.util.HostHolder;
import com.CampusForum.community.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DiscussPostServiceImpl implements DiscussPostService {

    @Autowired
    private SensitiveFilter sensitiveFilter;
    @Autowired
    private DiscussPostMapper discussPostMapper;
    @Autowired
    private HostHolder hostHolder;
    @Autowired
    private UserMapper userMapper;

    /**
     * 获取文章
     */
    public List<DiscussPost> getDiscussPosts(int userId, int offset, int limit) {
        return discussPostMapper.selectDiscussPosts(userId, offset, limit);
    }

    /**
     * 获取文章数目
     */
     public int getDiscussPostCount(int userId) {
        return discussPostMapper.selectDiscussPostRows(userId);
     }

    /**
     * 添加帖子
     */
    @Override
    public int addDiscussPost(DiscussPost discussPost) {
        // 转义html标识符
        discussPost.setTitle(HtmlUtils.htmlEscape(discussPost.getTitle()));
        discussPost.setContent(HtmlUtils.htmlEscape(discussPost.getContent()));

        // 处理敏感数据
        discussPost.setTitle(sensitiveFilter.filter(discussPost.getTitle()));
        discussPost.setContent(sensitiveFilter.filter(discussPost.getContent()));
         return discussPostMapper.insertDiscussPost(discussPost);
    }

    /**
     * 根据帖子id获取帖子
     */
    @Override
    public Map<String, Object> getDiscussPostById(int discussPostId) {
        Map<String, Object> map = new HashMap<>();
        DiscussPost discussPost = discussPostMapper.getDiscussPostById(discussPostId);
        System.out.println("discussPost = " + discussPost);
        map.put("post", discussPost);
        User user = userMapper.getById(discussPost.getUserId());
        map.put("user", user);

        return map;
    }


}
